package com.ouman.luoliluoli;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ouman.luoliluoli.galleryfragments.GalleryFragmentPagerAdapter;
import com.ouman.luoliluoli.galleryfragments.HotGirlsFragment;
import com.ouman.luoliluoli.galleryfragments.MeiziFragment;
import com.ouman.luoliluoli.galleryfragments.UnsplashFragment;


public class GalleryFragment extends Fragment {

    private int themeColor;

    public GalleryFragment() {
        // Required empty public constructor
    }

    public static GalleryFragment newInstance(String param1, String param2) {
        GalleryFragment fragment = new GalleryFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        themeColor = typedValue.data;
        System.out.println(themeColor);
        System.out.println("[IMPORTANT] got activity primary color: " + themeColor);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        ((MainActivity) getActivity()).setAppBarElevation(0);
        ((MainActivity) getActivity()).disableCollapsingLayoutTitle();

        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("美图");

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_tab_gallery);
        setupViewPager(viewPager);


        //find tab layout in article fragment
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_gallery);
        tabLayout.setBackgroundColor(themeColor);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setElevation(16);

        return view;
    }

    private void setupViewPager(ViewPager viewPager) {
        // !!!!!!!!!!!!!!!!!!!!!
        // !!!!!!!!!!!!!!!!!!!!
        // !!!!!!!!!!!!!!!!!!!!!!!
        // 这里真的要注意了！！！第二次不显示内容的问题在于这里要穿入getChildFragmentManager
        // 而不是getFragmentManager！！！！！
        // 尼玛在这里浪费了一下午时间噢
        GalleryFragmentPagerAdapter adapter = new GalleryFragmentPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new MeiziFragment(), "妹纸");
//        adapter.addFragment(new UnsplashFragment(), "Unsplash");
        adapter.addFragment(new HotGirlsFragment(), "HOT GIRLS");
        viewPager.setAdapter(adapter);
    }

}
